# Reproduce results in the main text and appendix
# Setup ----

library(tidyverse)
library(bizdays)
library(furrr)
library(fixest)
library(marginaleffects)

`%!in%` <- Negate(`%in%`)
source('aux/event.R')
safe_estim_diff <- safely(estim_diff)

theme_set(theme(legend.position = 'bottom',
                legend.direction = "horizontal",
                axis.line = element_line(color = 'black', linewidth = 0.3),
                panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
                text = element_text(size = 17),
                legend.text = element_text(size = 15),
                panel.background = element_rect(fill = "transparent", color = NA),
                plot.background = element_rect(fill = "transparent", color = NA),
                legend.background = element_rect(fill = "transparent", color = NA),
                legend.box.background = element_rect(fill = "transparent", color = NA),
                legend.key = element_rect(fill = "transparent", color = NA),
                legend.key.width = unit(1.8, 'cm')))

# Create business calendar (exclude business days)
business_calendar <- create.calendar('biz_calendar', weekdays = c('saturday','sunday'))

# Import data ----
data <- read_csv('data_out/analysis.csv.gz')
data31 <- read_csv('data_out/analysis_31d.csv.gz')
data181 <- read_csv('data_out/analysis_181d.csv.gz')
data_stoxx600 <- read_csv('data_out/analysis_stoxx600.csv.gz')

# import S&P 500 index and get a December 2021 baseline too for Figure A.1
SP500 <- read_csv('data/value_SP500.csv.gz')

# import file coding overlap with Voeten 2025:
overlap <- read_csv("data/Voeten2025_overlap.csv") # manually merged with Voeten based on company names and Google searches

# Main text ----
## Sample description ----
length(unique(data$ticker)) # 600 companies, Europe and non

# of which:
data %>%
  select(company, sample, Europe) %>%
  distinct() %>%
  select(sample, Europe) %>%
  table()
#                   Europe
# sample               0   1
#   Fossil fuel      343  66
#   Renewable energy 142  49

## Check overlap with Voeten 2025 ----
overlap <- overlap %>%
  mutate(our_green = case_when(sample == "Renewable energy" ~ 1,
                               sample == "Fossil fuel" ~ 0))

table("Voeten (2025) coding renewables" = overlap$Voeten_renewables, 
      "Our coding renewables" = overlap$our_green)
# among the firms that overlap with Voeten's sample, none of the firms we code as fossil fuel are coded as green by Voeten
# which is good because it means that those that we have in fossil are fossil indeed. 4 of those that overlap are 
# classified by us as green while Voeten doesn't. They're mostly in the NAICS category "other electric power generation":

overlap %>%
  filter(our_green == 1 & Voeten_renewables == 0) %>%
  left_join(data %>%
              select(company, NAICS_name) %>%
              distinct()) %>%
  select(company, Voeten_company, Voeten_renewables, our_green, NAICS_name)
# 3/4 are indeed in "other electric power generation"

# get their names (we'll drop them in a robustness check):
drop <- overlap %>%
  filter(our_green == 1 & Voeten_renewables == 0) %>%
  select(company) %>%
  pull()

## Figure 1 ----
# create a dataframe with events of interest for plotting
events <- data.frame(date = c('2022-02-24', 
                              '2022-02-25', 
                              '2022-02-28',
                              '2022-03-08', 
                              '2022-03-15', 
                              '2022-05-18',
                              '2022-10-18'),
                     event = c('Ukraine Invasion', 
                               'Sanction Package 2', 
                               'Sanction Package 3',
                               'REPowerEU Plan Proposal', 
                               'Sanction Package 4', 
                               'REPowerEU Presentation',
                               'REPowerEU Gas price cap'),
                     label = c('Ukraine Invasion',
                               'Export ban on Russian\ngoods and technologies\nin fuel refining', 
                               'Prohibition of transactions\nwith Russian Central Bank\nto affect fuel exports',
                               'Commission declares plan to phase\nout dependency on Russian gas, oil, and coal\n(diversification of supply and renewables)',
                               'Restriction of investment into\nthe Russian fuel industry and\nexports of technology and energy services',
                               'A plan aimed at saving gas,\nproducing clean energy, and\ndiversifying gas supplies',
                               'In the context of REPowerEU, Commission\nproposes a revenue cap for energy producers\nand limit on oil excess profits'),
                     y = c(0.45,
                           -0.55,
                           0.85,
                           -1.55,
                           1.55,
                           -0.85,
                           0.55),
                     yseg = c(0.35,
                              -0.25,
                              0.55,
                              -1.25,
                              1.25,
                              -0.55,
                              0.20)) %>%
  mutate(date = as.Date(date),
         status = case_when(date == '2022-02-24' ~ 'Ukraine\ninvasion',
                            date %in% as.Date(c('2022-02-25', '2022-02-28', '2022-03-15')) ~ 'Sanction\npackage',
                            TRUE ~ 'REPowerEU\nmilestone'),
         status = factor(status, levels = c('Ukraine\ninvasion', 'REPowerEU\nmilestone', 'Sanction\npackage')),
         label = case_when(event == 'Ukraine Invasion' ~ 'Ukraine Invasion',
                           TRUE ~ paste0(event, ':\n', label)),
         month = lubridate::month(date),
         year = lubridate::year(date))

# create a month-dataframe for plotting labels
month_date_range <- seq(as.Date("2021-12-01"), as.Date("2023-01-01"), by = "month")
month_format <- format(month_date_range, '%b')
month_df <- data.frame(month_date_range, month_format)

# create a year-dataframe for plotting labels
year_date_range <- c("2021-01-01", "2022-01-01") %>% as.Date()
year_date_range <- lubridate::ceiling_date(year_date_range, 'year')
year_format <- format(year_date_range, '%Y')
year_df <- data.frame(year_date_range, year_format)

events %>%
  ggplot(aes(x = date, y = 0, col = status, label = label)) +
  # plot black thick time line:
  geom_hline(yintercept = 0, 
             color = "black", linewidth = 0.3) +
  # add segments for events:
  geom_segment(aes(y = yseg, yend = 0, xend = date), color = 'black', linewidth = 0.2) +
  # add events as points:
  geom_point(size = 2) +
  # add event labels:
  geom_text(aes(y = y,
                label = label),
            size = 4.6,
            show.legend = FALSE) +
  # add month labels:
  geom_text(data = month_df,
            aes(x = month_date_range,
                y = -0.1,
                label = month_format),
            size = 4, vjust = 0.5, color = 'black', angle = 90) +
  # add year labels:
  geom_text(data = year_df,
            aes(x = year_date_range,
                y = -0.3,
                label = year_format),
            size = 4, color = 'black') +
  # take care of point colors:
  scale_color_discrete('') +
  # set vertical limits to better read labels:
  coord_cartesian(ylim = c(-1.5, 2)) +
  # remove all axis stuff:
  theme(axis.line.y = element_blank(),
        axis.text.y = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank(),
        legend.position = c(0.78,0.2),
        legend.box.background = element_rect(colour = "black")) +
  guides(col = guide_legend(label.position = 'bottom'))
ggsave('plots/figure_1.pdf', width = 10, height = 7)

## Figure 2 ----
d <- data %>%
  filter(date >= "2021-12-01" &
           date <= "2021-12-31") %>%
  group_by(company) %>%
  reframe(avg_dec = mean(price_close, na.rm = TRUE),
          sample = unique(sample),
          Europe = unique(Europe),
          NAICS_name = unique(NAICS_name)) %>% 
  select(company, sample, Europe, NAICS_name, avg_dec) %>%
  distinct() %>%
  group_by(sample, Europe, NAICS_name) %>%
  mutate(n = n(),
         sample = factor(sample, levels =  c("Fossil fuel", "Renewable energy"))) 

# show sizes:
d %>%
  select(-avg_dec, -company) %>% 
  distinct() %>%
  arrange(-n)

ord <- data %>%
  filter(Europe == 1) %>%
  select(company, sample, NAICS_name) %>%
  distinct() %>%
  group_by(NAICS_name) %>%
  reframe(n = n(),
          sample = unique(sample)) %>%
  mutate(sample = factor(sample, c("Renewable energy", "Fossil fuel"))) %>%
  arrange(sample, n) %>%
  ungroup() %>%
  select(NAICS_name) %>%
  pull()

f <- 3
set.seed(160493)
d %>%
  filter(Europe == 1) %>%
  mutate(NAICS_name = factor(NAICS_name, levels = ord)) %>%
  ggplot(aes(y = NAICS_name)) +
  geom_histogram(data = . %>% select(-avg_dec, -company) %>% distinct() %>% mutate(n = n*f),
                 aes(x = n, fill = sample), stat = "identity", alpha = .8) +
  geom_point(aes(x = avg_dec), position = position_jitter(height = 0.15), size = 2,
             shape = 4) +
  xlab(latex2exp::TeX("Average share price at market close, December 2021 ($\\times$)")) + ylab("") +
  scale_x_continuous(labels = function(x) paste0("$", x),
                     sec.axis = sec_axis(~ . / f, name = "Number of firms (Histograms)")) +
  scale_fill_manual("", values = c("forestgreen", "tan3"),
                    breaks = c("Renewable energy", "Fossil fuel")) 
ggsave("plots/figure_2.pdf", width = 11, height = 5)

## Figure 3 ----
labels <- data.frame(date = c(unique(data$war) - 4, 
                              unique(data$package_2) - 3.5,
                              unique(data$package_3) - 3),
                     returns = c(2, 3, 4),
                     label = c('Ukraine\ninvasion', 'Package 2', 'Package 3'),
                     sample = 'Renewable energy')
arrows <- data.frame(date = c(unique(data$war) - 2, 
                              unique(data$package_2) - 2,
                              unique(data$package_3) - 2),
                     xend = c(unique(data$war) - 0.2,
                              unique(data$package_2) - 0.4,
                              unique(data$package_3) - 0.2),
                     returns = c(2, 2.9, 3.9),
                     yend = c(2, 2.6, 3.7),
                     sample = 'Renewable energy')

data %>%
  filter(date >= offset(war, -20, business_calendar) &
           date <= package_3) %>%
  mutate(exp_chg = case_when(date <= package_2 ~ exp_chg_lasso_package_2,
                             date == package_3 ~ exp_chg_lasso_package_3),
         abn_chg = chg - exp_chg) %>%
  filter(!is.na(abn_chg)) %>%
  group_by(date, sample) %>%
  summarise(chg = mean(chg, na.rm = TRUE),
            exp_chg = mean(exp_chg, na.rm = TRUE)) %>%
  pivot_longer(cols = 3:4, names_to = 'series', values_to = 'returns') %>%
  filter(!is.na(returns) & !is.nan(returns)) %>%
  as.data.frame() %>%
  mutate(sample = factor(sample, levels = c('Renewable energy', 'Fossil fuel'))) %>%
  ggplot(aes(x = date, y = returns)) +
  geom_hline(yintercept = 0, linetype = 'dotted', linewidth = 0.5) +
  geom_vline(data = data.frame(date = c(unique(data$war),
                                        unique(data$package_2),
                                        unique(data$package_3))),
             aes(xintercept = date),
             linetype = 'dotted', linewidth = 0.5) +
  geom_line(aes(linetype = series)) +
  geom_point(aes(shape = series), fill = 'white') +
  geom_label(data = labels %>%
               mutate(sample = factor(sample, levels = c('Renewable energy', 'Fossil fuel'))),
             aes(label = label),
             label.size = NA,
             color = "black", lwd = 4) +
  geom_segment(data = arrows %>%
                 mutate(sample = factor(sample, levels = c('Renewable energy', 'Fossil fuel'))),
               aes(xend = xend, yend = yend),
               arrow = arrow(length = unit(0.2, "cm")),
               linewidth = .5, col = I("black")) +
  facet_wrap('sample') +
  scale_y_continuous(labels = function(x) paste0(x, '%')) +
  scale_linetype_manual('', values = c('solid', 'dashed'),
                        labels = c('Observed Returns', 'Counterfactual Returns')) +
  scale_shape_manual('', values = c(19, 21),
                     labels = c('Observed Returns', 'Counterfactual Returns')) +
  xlab('') + ylab('Returns (daily % change in stock price)') +
  guides(shape = guide_legend(override.aes = list(size = 2)))
ggsave('plots/figure_3.pdf', width = 10, height = 6)

## Figure 4 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',             
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data %>%
             filter(Europe == 1)) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  filter(event_type != "REPowerEU Milestones") %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_label(aes(col = sample, label = paste0(format(round(estimate, 2), nsmall = 2), "%"), x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  coord_cartesian(xlim = c(-3.5, 8)) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_4.pdf", width = 7, height = 4)

## Figure 5 ----
out %>%
  filter(event_type == "REPowerEU Milestones") %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_label(aes(col = sample, label = paste0(format(round(estimate, 2), nsmall = 2), "%"), x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  coord_cartesian(xlim = c(-3.5, 8)) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_5.pdf", width = 7, height = 4)

# Appendix ----
## Figure A.1 ----
# first, remove public holidays that are not in bizcal:
holidays_2022 <- as.Date(c(# NYSE first:
  "2022-01-17", # Martin Luther King, Jr. Day
  "2022-02-21", # President's day
  "2022-04-15", # Good Friday
  "2022-05-30", # Memorial Day
  "2022-06-20", # Juneteenth was also added in 2021: https://www.nasdaq.com/articles/is-the-stock-market-closed-for-juneteenth-2022
  "2022-07-04", # 4th July
  "2022-09-05", # labor day
  "2022-11-24", # Thanksgiving
  "2022-12-25", # Christmas
  # EURONEXT (the ones we haven't put already):
  "2022-01-03", # New Year's day
  "2022-04-18", # Easter Monday
  "2022-12-26" # Stephen’s Day/Boxing Day
))

# we need a December 2021 baseline. How many companies *do not* have information on the 31 December 2021?
comps <- data %>%
  filter(date == as.Date("2021-12-31")) %>%
  filter(!is.na(price_close)) %>%
  select(ticker) %>%
  pull() %>%
  unique()

unique(data$ticker[which(data$ticker %!in% comps)])
# these are companies where we are missing December 31 information. It's 190 firms
# Get the latest possible day in December where we have information for every company in the sample

baselines <- data %>%
  filter(date >= as.Date("2021-12-01") &
           date <= as.Date("2021-12-31")) %>%
  filter(!is.na(price_close)) %>%
  # get the last December 2021 date per company:
  arrange(ticker, date) %>%
  group_by(ticker) %>%
  filter(row_number() == n()) %>%
  select(ticker, date, price_close, volume, sample)

# for some it goes back as early as December 01 2021:
table(baselines$date)[sort(names(table(baselines$date)))] 

# get December 2021 baseline stock price and volume trading by sample:
baselines <- baselines %>%
  group_by(sample) %>%
  reframe(dec21_avg_price = mean(price_close, na.rm = TRUE),
          dec21_avg_volume = mean(volume, na.rm = TRUE))

baselines <- baselines %>%
  bind_rows(SP500 %>%
              filter(date == "2021-12-31") %>%
              rename(dec21_avg_price = close) %>%
              select(-date)) %>%
  select(-dec21_avg_volume)

data %>%
  filter(date > as.Date('2021-12-31') &
           date <= as.Date('2023-01-01')) %>%
  filter(is.bizday(date, business_calendar)) %>%
  # remove public holidays observed on NYSE and Euronext
  filter(date %!in% holidays_2022) %>%
  group_by(sample, date) %>%
  summarise(close = mean(price_close, na.rm = TRUE)) %>%
  filter(!is.na(close) & !is.nan(close)) %>%
  rbind(SP500 %>%   
          filter(date > as.Date('2021-12-31') &
                   date <= as.Date('2023-01-01'))) %>%
  left_join(baselines, by = "sample") %>%
  # add the 100 baseline of December 2021 for the beginning of the plot
  rbind(baselines %>% mutate(date = as.Date("2021-12-31"), 
                             close = dec21_avg_price)) %>%
  mutate(norm_close = 100*close/dec21_avg_price) %>%
  ggplot(aes(x = date, y = norm_close)) +
  geom_line(aes(color = sample)) +
  geom_hline(yintercept = 100, linewidth = 0.5, linetype = 'dotted') +
  geom_vline(xintercept = unique(data$war),linetype = 'dotted') +
  ggrepel::geom_label_repel(data = data.frame(x = unique(data$war),
                                              y = 150,
                                              label = "Invasion of\nUkraine"),
                            mapping = aes(x = x, y = y, label = label),
                            min.segment.length = 0,
                            label.size = NA,
                            nudge_x = -30, nudge_y = 0,
                            arrow = arrow(length = unit(0.1, units = "inches")),
                            fill = NA) +
  ylab('Normalized stock price (100 = price as of 31 Dec 2021)') + xlab('') +
  scale_color_manual('', values = c('tan3', 'forestgreen', 'dodgerblue2'))
ggsave('plots/figure_A1.pdf', width = 10, height = 7)

## Figure A.2 ----
d <- data %>%
  filter(date >= "2021-12-01" &
           date <= "2021-12-31") %>%
  group_by(company) %>%
  reframe(avg_dec = mean(price_close, na.rm = TRUE),
          sample = unique(sample),
          Europe = unique(Europe),
          NAICS_name = unique(NAICS_name)) %>% 
  select(company, sample, Europe, NAICS_name, avg_dec) %>%
  distinct() %>%
  group_by(sample, Europe, NAICS_name) %>%
  mutate(n = n(),
         sample = factor(sample, levels =  c("Fossil fuel", "Renewable energy"))) 

ord <- data %>%
  filter(Europe == 0) %>%
  select(company, sample, NAICS_name) %>%
  distinct() %>%
  group_by(NAICS_name) %>%
  reframe(n = n(),
          sample = unique(sample)) %>%
  mutate(sample = factor(sample, c("Renewable energy", "Fossil fuel"))) %>%
  arrange(sample, n) %>%
  ungroup() %>%
  select(NAICS_name) %>%
  pull()

d %>%
  filter(Europe == 0) %>%
  mutate(NAICS_name = factor(NAICS_name, levels = ord)) %>%
  ggplot(aes(y = NAICS_name)) +
  geom_histogram(data = . %>% select(-avg_dec, -company) %>% distinct() %>% mutate(n = n*f),
                 aes(x = n, fill = sample), stat = "identity", alpha = .8) +
  geom_point(aes(x = avg_dec), position = position_jitter(height = 0.15), size = 2,
             shape = 4) +
  xlab(latex2exp::TeX("Average share price at market close, December 2021 ($\\times$)")) + ylab("") +
  scale_x_continuous(labels = function(x) paste0("$", x),
                     sec.axis = sec_axis(~ . / f, name = "Number of firms (Histograms)")) +
  scale_fill_manual("", values = c("forestgreen", "tan3"),
                    breaks = c("Renewable energy", "Fossil fuel"))
ggsave("plots/figure_A2.pdf", width = 11, height = 5)

## Table A.1 ---- 
tab <- data %>%
  filter(Europe == 1) %>%
  filter(sample == "Fossil fuel") %>%
  mutate(company = str_replace_all(company, "\\&", "\\\\&"),
         ex_name = case_when(ex_name == "WARSAW STOCK EXCHANGE/EQUITIES/MAIN MARKET" ~ "WARSAW STOCK EXCHANGE",
                             ex_name == "WIENER BOERSE AG AMTLICHER HANDEL (OFFICIAL MARKET)" ~ "WIENER BOERSE",
                             ex_name == "BULGARIAN STOCK EXCHANGE - MAIN MARKET" ~ "BULGARIAN STOCK EXCHANGE",
                             ex_name == "LJUBLJANA STOCK EXCHANGE (OFFICIAL MARKET)" ~ "LJUBLJANA STOCK EXCHANGE",
                             ex_name == "OPERADOR DE MERCADO IBERICO DE ENERGIA - PORTUGAL" ~ "EURONEXT - EURONEXT LISBON",
                             ex_name == "ELECTRONIC SHARE MARKET" ~ "BORSA ITALIANA",
                             ex_name == "ATHENS EXCHANGE S.A. CASH MARKET" ~ "ATHENS EXCHANGE",
                             TRUE ~ ex_name)) %>%
  select(company, country_hq, ex_name, NAICS_name) %>%
  distinct() %>%
  arrange(company) %>%
  rename("Company name" = 1,
         "Headquarter country" = 2,
         "Primary exchange" = 3,
         "NAICS-6 code name" = 4) %>%
  mutate(N = row_number()) %>%
  relocate(N)

tab %>%
  filter(N <= 33) %>%
  kableExtra::kbl(longtable = FALSE, booktabs = TRUE, 
                  col.names = gsub("\\.+\\d", "", names(.)),
                  caption = "List of traded European companies in the fossil fuel sample (Firms 1--33) \\label{table:sample_fossil_EU}",
                  position = "ht", format = "html", escape = FALSE) %>%
  kableExtra::kable_styling(font_size = 9) %>%
  kableExtra::save_kable("tables/table_A1.html")

## Table A.2 ----
tab %>%
  filter(N > 33) %>%
  kableExtra::kbl(longtable = FALSE, booktabs = TRUE, 
                  col.names = gsub("\\.+\\d", "", names(.)),
                  caption = "List of traded European companies in the fossil fuel sample (Firms 34--66) \\label{table:sample_fossil_EU2}",
                  position = "ht", format = "html", escape = FALSE) %>%
  kableExtra::kable_styling(font_size = 9) %>%
  kableExtra::save_kable("tables/table_A2.html")

## Table A.3 ----
tab <- data %>%
  filter(Europe == 1) %>%
  filter(sample != "Fossil fuel") %>%
  mutate(company = str_replace_all(company, "\\&", "\\\\&"),
         ex_name = case_when(ex_name == "WARSAW STOCK EXCHANGE/EQUITIES/MAIN MARKET" ~ "WARSAW STOCK EXCHANGE",
                             ex_name == "WIENER BOERSE AG AMTLICHER HANDEL (OFFICIAL MARKET)" ~ "WIENER BOERSE",
                             ex_name == "BULGARIAN STOCK EXCHANGE - MAIN MARKET" ~ "BULGARIAN STOCK EXCHANGE",
                             ex_name == "LJUBLJANA STOCK EXCHANGE (OFFICIAL MARKET)" ~ "LJUBLJANA STOCK EXCHANGE",
                             ex_name == "OPERADOR DE MERCADO IBERICO DE ENERGIA - PORTUGAL" ~ "EURONEXT - EURONEXT LISBON",
                             ex_name == "ELECTRONIC SHARE MARKET" ~ "BORSA ITALIANA",
                             ex_name == "ATHENS EXCHANGE S.A. CASH MARKET" ~ "ATHENS EXCHANGE",
                             TRUE ~ ex_name)) %>%
  select(company, country_hq, ex_name, NAICS_name) %>%
  distinct() %>%
  arrange(company) %>%
  rename("Company name" = 1,
         "Headquarter country" = 2,
         "Primary exchange" = 3,
         "NAICS-6 code name" = 4) %>%
  mutate(N = row_number()) %>%
  relocate(N) 

tab %>%
  filter(N <= 25) %>%
  kableExtra::kbl(longtable = FALSE, booktabs = TRUE, 
                  col.names = gsub("\\.+\\d", "", names(.)),
                  caption = "List of traded EU companies in the renewable energy sample (Firms 1 -- 25) \\label{table:sample_renew_EU}",
                  position = "ht", format = "html", escape = FALSE) %>%
  kableExtra::kable_styling(font_size = 9) %>%
  kableExtra::save_kable("tables/table_A3.html")

## Table A.4 ----
tab %>%
  filter(N > 25) %>%
  kableExtra::kbl(longtable = FALSE, booktabs = TRUE, 
                  col.names = gsub("\\.+\\d", "", names(.)),
                  caption = "List of traded EU companies in the renewable energy sample (Firms 26 -- 49) \\label{table:sample_renew_EU2}",
                  position = "ht", format = "html", escape = FALSE) %>%
  kableExtra::kable_styling(font_size = 9) %>%
  kableExtra::save_kable("tables/table_A4.html")

## Figure B.1 ----
win <- 10
out <- map(.x = c('package_2', 'package_3', 'package_4',             
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_car_diff,
           d = data %>%
             filter(Europe == 1),
           size = win,
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.error,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.error,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.error,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.error)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_B1.pdf", width = 9, height = 4)

## Figure D.1 ----
out_stoxx600 <- map(.x = c('package_2', 'package_3', 'package_4',             
                           'repowereu_1', 'repowereu_2', 'repowereu_3'),
                    .f = estim_diff,
                    .progress = TRUE,
                    d = data_stoxx600 %>%
                      filter(Europe == 1)) %>%
  bind_rows()

out_stoxx600 <- out_stoxx600 %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out_stoxx600 %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  coord_cartesian(xlim = c(-3.5, 8)) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D1.pdf", width = 9, height = 4)

## Figure D.2 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',             
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data %>%
             filter(Europe == 0)) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  coord_cartesian(xlim = c(-4, 8)) +
  scale_x_continuous(labels = function(x) paste0(x, '%'), breaks = c(-4, 0, 4, 8)) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D2.pdf", width = 9, height = 4)

## Figure D.3 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data %>%
             filter(Europe_based == 1)) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D3.pdf", width = 9, height = 4)

## Figure D.4 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data %>%
             filter(Europe_traded == 1)) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D4.pdf", width = 9, height = 4)

## Figure D.5 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data %>%
             filter(EU == 1)) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D5.pdf", width = 9, height = 4)

## Figure D.6 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data %>%
             filter(Europe == 1 &
                      company %!in% drop)) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D6.pdf", width = 9, height = 4)

## Figure D.7 ----
lev <- 0.10
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = function(x) {
             data %>% 
               filter(across(.cols = paste0("R2_lasso_", x),
                             .fns = ~.x > lev)) %>%
               filter(Europe == 1) %>%
               estim_diff(event_name = x, d = .)},
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D7.pdf", width = 9, height = 4)

## Figure D.8 ----
lev <- 0.30
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = function(x) {
             data %>% filter(across(.cols = paste0("R2_lasso_", x),
                                    .fns = ~.x > lev)) %>%
               filter(Europe == 1) %>%
               estim_diff(event_name = x, d = .)},
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D8.pdf", width = 9, height = 4)

## Figure D.9 ----
lev <- 0.50
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = function(x) {
             data %>% filter(across(.cols = paste0("R2_lasso_", x),
                                    .fns = ~.x > lev)) %>%
               filter(Europe == 1) %>%
               estim_diff(event_name = x, d = .)},
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D9.pdf", width = 9, height = 4)

## Figure D.10 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data %>%
             filter(Europe == 1),
           type_count = "ols") %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D10.pdf", width = 9, height = 4)

## Figure D.11 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data31 %>%
             filter(Europe == 1)) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  coord_cartesian(xlim = c(-3.5, 8)) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D11.pdf", width = 9, height = 4)

## Figure D.12 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data181 %>%
             filter(Europe == 1)) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  coord_cartesian(xlim = c(-3.5, 8)) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D12.pdf", width = 9, height = 4)

## Figure D.13 ----
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_diff,
           .progress = TRUE,
           d = data %>%
             filter(Europe == 1),
           dep.var = "chg") %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.err,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.err,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.err,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.err)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D13.pdf", width = 9, height = 4)

## Figure D.14 ----
win <- 10
lev <- 0.10
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = function(x) {
             data %>% filter(across(.cols = paste0("R2_lasso_", x),
                                    .fns = ~.x > lev)) %>%
               filter(Europe == 1) %>%
               estim_car_diff(event_name = x, d = ., size = win)},
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.error,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.error,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.error,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.error)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D14.pdf", width = 9, height = 4)

## Figure D.15 ----
lev <- 0.30
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = function(x) {
             data %>% filter(across(.cols = paste0("R2_lasso_", x),
                                    .fns = ~.x > lev)) %>%
               filter(Europe == 1) %>%
               estim_car_diff(event_name = x, d = ., size = win)},
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.error,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.error,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.error,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.error)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D15.pdf", width = 9, height = 4)

## Figure D.16 ----
lev <- 0.50
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = function(x) {
             data %>% filter(across(.cols = paste0("R2_lasso_", x),
                                    .fns = ~.x > lev)) %>%
               filter(Europe == 1) %>%
               estim_car_diff(event_name = x, d = ., size = win)},
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.error,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.error,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.error,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.error)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D16.pdf", width = 9, height = 4)

## Figure D.17 ----
win <- 20
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_car_diff,
           d = data %>%
             filter(Europe == 1),
           size = win,
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.error,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.error,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.error,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.error)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D17.pdf", width = 9, height = 4)

## Figure D.18 ----
win <- 5
out <- map(.x = c('package_2', 'package_3', 'package_4',
                  'repowereu_1', 'repowereu_2', 'repowereu_3'),
           .f = estim_car_diff,
           d = data %>%
             filter(Europe == 1),
           size = win,
           .progress = TRUE) %>%
  bind_rows()

out <- out %>%
  mutate(event_name = case_when(event == 'war' ~ 'Ukraine\nInvasion',
                                event == 'package_2' ~ 'Sanction\nPackage 2',
                                event == 'package_3' ~ 'Sanction\nPackage 3',
                                event == 'repowereu_1' ~ 'REPowerEU\nPlan Proposal',
                                event == 'package_4' ~ 'Sanction\nPackage 4',
                                event == 'repowereu_2' ~ 'REPowerEU\nPresentation',
                                event == 'repowereu_3' ~ 'REPowerEU\nGas price cap'),
         event_type = str_remove(event_name, "\\n.+"),
         event_name = paste0(event_name, '\n(', date, ')'),
         event_name = fct_reorder(event_name, date, .desc = TRUE)) %>%
  mutate(signif = diff.pval < 0.05,
         event_type = factor(event_type, levels = c("Sanction", "REPowerEU"), 
                             labels = c("Sanction Packages", "REPowerEU Milestones")),
         conf.low = estimate - qnorm(1-(1-0.95)/2)*std.error,
         conf.high = estimate + qnorm(1-(1-0.95)/2)*std.error,
         conf2.low = estimate - qnorm(1-(1-0.834)/2)*std.error,
         conf2.high = estimate + qnorm(1-(1-0.834)/2)*std.error)

out %>%
  ggplot(aes(y = event_name)) +
  geom_vline(xintercept = 0, linewidth = 0.5, linetype = 'dotted') +
  ggstance::geom_linerangeh(aes(xmin = conf.low, xmax = conf.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange") +
  ggstance::geom_linerangeh(aes(xmin = conf2.low, xmax = conf2.high, col = sample),
                            position = position_dodge(width = 0.8), key_glyph = "pointrange", size = .8) +
  geom_point(aes(col = sample, x = estimate),
             position = position_dodge2(width = 0.8),
             show.legend = FALSE, size = 3) +
  facet_wrap("event_type", scales = "free_y") +
  ylab('') + xlab('Estimated event effect and confidence intervals') +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  scale_fill_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                    values = c('tan3', 'forestgreen')) +
  scale_color_manual('', breaks = c("Fossil fuel", "Renewable energy"),
                     values = c('tan3', 'forestgreen')) +
  guides(col = guide_legend(override.aes = list(shape = 19, size = 0.7)))
ggsave("plots/figure_D18.pdf", width = 9, height = 4)

#====# The End #====#